package com.larry.tinyweb.util.proxy;

import com.larry.tinyweb.plugin.proxy.Transaction;
import com.larry.tinyweb.plugin.proxy.TransactionInvocationHandler;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * Created by larry on 16/12/11.
 *
 * @author larry
 */
public class ProxyUtil {
    public static Object proxyFor(Class proxyInterface, Object proxyTarget, InvocationHandler invocationHandler) {
        return Proxy.newProxyInstance(
                proxyTarget.getClass().getClassLoader(),
                new Class[]{proxyInterface}, invocationHandler);
    }


    public static Object proxyForTransaction(Class proxyInterface, Object proxyTarget) {
        return Proxy.newProxyInstance(
                proxyTarget.getClass().getClassLoader(),
                new Class[]{proxyInterface},
                new TransactionInvocationHandler(proxyTarget, new Transaction()));
    }
}
